package docmodel;

import java.io.File;
import java.io.IOException;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.SAXException;

import org.apache.xerces.parsers.DOMParser;	//XERCES

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


/**
 * 
 * @author andrea
 *
 */
public class XDP_Reader 
{
	/** */
	private DOMParser parser;
	
	/** */
	private static Document dom;
	
	/** */
	private XDP_Liste liste = new XDP_Liste();
	
	/**
	 * 	 COSTRUTTORE 1
	 * 
	 * @param filename
	 */
	public XDP_Reader (String filename) 
	{
		
		parser = new DOMParser();
	    
		try 
		{
			parser.parse(filename);
		} 
	    catch (SAXException e) {
			e.printStackTrace();
		}
		catch (IOException e){
			e.printStackTrace();
		}
	    
	    dom = parser.getDocument(); 
	}

	/**
	 * COSTRUTTORE 2
	 * 
	 * @param filename
	 */
	public XDP_Reader () 
	{

	}
	
	/**
	 * 
	 * @return
	 */
	public XDP_Liste getListe() {
		return liste;
	}

	/**
	 * 
	 * @return
	 */
	public String run () 
	{
		scorriNodo(dom.getDocumentElement());
		
		return "message";
	}
	
	/**
	 * 
	 * @param theNode
	 */
	private void scorriNodo  (Node theNode) 
	{
		NodeList children = theNode.getChildNodes();
	    
		stampaNodo(theNode);
	    
	    for (int i = 0; i < children.getLength(); i++) 
	    {
	    	Node aNode = children.item(i);
	    	
	    	if (aNode.hasChildNodes())
	    		scorriNodo(aNode);
	    	else 
	    	{
	    		String name = aNode.getNodeName();
	    		String value = aNode.getNodeValue(); 
	    		
	    		stampaNodo(name,value);
	    		
	    		if( is_textNode(aNode) ) 
	    		{	
	    			cercaMarker(aNode);	
	    			//System.out.println("nodo testo TROVATO");
	    		}
	    	}
	    }
	}
	
    /**
     * 
     * @param aNode
     */
	private static void stampaNodo(Node aNode)
	{
	    
	    String name = aNode.getNodeName();
	    String value = aNode.getNodeValue(); 
	    
	    System.out.println(name + "," + value);
	}

	/**
	 * 
	 * @param name
	 * @param value
	 */
	private void stampaNodo(String name, String value)
	{   
	    System.out.println(name + "," + value);
	}
	
	
    /**
     * 
     * @param aNode
     */
	private boolean is_textNode(Node aNode)
	{
		int tipoNodo = aNode.getNodeType();
	
		if(tipoNodo != Node.TEXT_NODE)
			return false;
		
		return true;
	}
   
	/**
	 * 
	 * @param aNode
	 */
	private void cercaMarker(Node aNode)
	{
		
		
		String text = aNode.getTextContent();
		/*
		System.out.println( 	"#Nome nodo: "+aNode.getNodeName() 
							+ 	", valore originale: "
							+	text 
							+	" | Valore sostituito: " 
							+ 	Marker.findMarker(text,null));
		*/
		Marker.findNodeMarker(aNode, liste);														//Marker.INSTANCE
			
	}
	
	/**
	 * 
	 * @param filename
	 */
	public void save(String filename)
	{
		
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = null;

		try {
			transformer = transformerFactory.newTransformer();
		}
		catch (TransformerConfigurationException e) {
			e.printStackTrace();
		}
		
		Node pageArea = dom.getElementsByTagName("pageArea").item(0);
		for(FloatingField ff : liste.listaFloatingFields) 
		{	
			pageArea.appendChild(ff.getFieldElement(dom));	
		}
		
		DOMSource source = new DOMSource(dom);								//doc
		StreamResult result = new StreamResult(new File(filename));
		
		try {
			transformer.transform(source, result);
		} 
		catch (TransformerException e) {
			e.printStackTrace();
		}		
	}


	/**
	 * 
	 * @param filename
	 * @return
	 */
	public String apriFile(String filename) 
	{
		parser = new DOMParser();
        
        //parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", true);
        //parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", true);
		
		try 
		{
			parser.parse(filename);
		} 
	    catch (SAXException e) {
			e.printStackTrace();
		}
		catch (IOException e){
			e.printStackTrace();
		}
	    
	    dom = parser.getDocument();
	    
		return "OK"; 
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		
		XDP_Reader reader = new XDP_Reader("prova.xdp");		
		//XDP_Reader reader = new XDP_Reader("simple.xdp");		
		
		reader.run();
		reader.save("simpleout.xdp");
		reader.liste.stampa_lista();
		
	}

}

